<template>
  <header class="header">
    <div class="nav-container">
      <div class="logo">
        <h1>课程学习网站</h1>
      </div>
      <div class="menu-wrapper">
        <template v-if="isLoggedIn">
          <el-button class="nav-button" @click="$router.push('/')">首页</el-button>
          <el-button class="nav-button" @click="$router.push('/add-course')">添加课程</el-button>
          <el-button class="nav-button" @click="$router.push('/add-category')">添加类别</el-button>
          <el-button type="primary" class="logout-button" @click="handleLogout">退出登录</el-button>
        </template>
        <template v-else>
          <el-button class="nav-button" @click="$router.push('/login')">登录</el-button>
          <el-button class="nav-button" @click="$router.push('/register')">注册</el-button>
        </template>
      </div>
    </div>
  </header>
</template>

<script>
import { ElMessage } from 'element-plus'
import { ref, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: 'Header',
  setup() {
    // 使用Vue Router进行页面跳转
    const router = useRouter()
    
    // 使用ref来创建响应式的登录状态
    const isLoggedIn = ref(!!localStorage.getItem('token'))
    
    // 监听登录成功事件
    const handleLoginEvent = () => {
      isLoggedIn.value = !!localStorage.getItem('token')
    }
    
    // 在组件挂载时添加事件监听器
    window.addEventListener('userLoggedIn', handleLoginEvent)
    
    // 退出登录方法
    const handleLogout = () => {
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      isLoggedIn.value = false
      ElMessage.success('退出登录成功')
      router.push('/login')
    }
    
    // 在组件卸载时移除事件监听器
    onUnmounted(() => {
      window.removeEventListener('userLoggedIn', handleLoginEvent)
    })
    
    return {
      isLoggedIn,
      handleLogout
    }
  }
}</script>

<style scoped>
.header {
  background-color: #409eff;
  color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  padding: 12px 0;
}

.menu-wrapper {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* 导航按钮样式 */
.nav-button {
  background-color: transparent;
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 4px;
  padding: 6px 16px;
  transition: all 0.3s ease;
}

.nav-button:hover {
  background-color: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.6);
  color: white;
}

/* 退出登录按钮样式 */
.logout-button {
  background-color: #f56c6c !important;
  border: none !important;
  white-space: nowrap;
}

.logout-button:hover {
  background-color: #f78989 !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    flex-direction: column;
    padding: 0 10px;
  }
  
  .logo h1 {
    font-size: 18px;
    padding: 8px 0;
  }
  
  .menu-wrapper {
    width: 100%;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
  }
  
  .nav-button,
  .logout-button {
    font-size: 12px;
    padding: 4px 12px;
  }
}
</style>